
const tool = require('../tool');
const { handleReplace } = require('../tool');

module.exports = login_register_generate;

async function login_register_generate(adminConfig, projectConfig) {

  await tool.testAndMakeDirectory({
    dirPath: '../src/pages/admin',  //目录要当前运行的文件夹为准
  });
  //登录注册页主要是api的替换
  for (var item in adminConfig) {
    var curItem = adminConfig[item];
    var itemKey = item;

    if (item == 'login') {
      //生成？
      if(curItem.isGenerate == "false") {
        return;
      }
      var loginTpl = require('../template/admin/login');
      loginTpl = tool.handleReplace(loginTpl, {
        loginTitle: curItem.loginTitle ? curItem.loginTitle : '自动生成登录项目',
        loginApi: curItem.loginApi ? curItem.loginApi : "API.admin.login",
        verifyCodeApi: curItem.verifyCodeApi ? curItem.verifyCodeApi : "API.admin.getVerifyCode",
      })
      tool.writeFile({
        outputPath: '../src/pages/admin/' + 'login' + '.vue',
        fileContent: loginTpl
      })
      continue;
    }
    if (item == 'register') {
      //生成？
      if(curItem.isGenerate == "false") {
        return;
      }
      var registerTpl = require('../template/admin/register');
      registerTpl = tool.handleReplace(registerTpl, {
        registerTitle: curItem.registerTitle ? curItem.registerTitle : '注册',
        registerApi: curItem.registerApi ? curItem.registerApi : 'API.admin.register',
        getPhoneVerifyCode: curItem.getPhoneVerifyCode ? curItem.getPhoneVerifyCode : "API.common.getCode",
      })
      tool.writeFile({
        outputPath: '../src/pages/admin/' + 'register' + '.vue',
        fileContent: registerTpl
      })
      continue;
    }
    if (item == 'resetPwd') {
      //生成？
      if(curItem.isGenerate == "false") {
        return;
      }
      var resetPwdTpl = require('../template/admin/resetPwd');
      resetPwdTpl = tool.handleReplace(resetPwdTpl, {
        resetPwdTitle: curItem.resetPwdTitle ? curItem.resetPwdTitle : '修改密码',
        resetApi: curItem.resetApi ? curItem.resetApi : "API.admin.modify_pwd",
      })
      tool.writeFile({
        outputPath: '../src/pages/admin/' + 'resetPwd' + '.vue',
        fileContent: resetPwdTpl
      })
      continue;

    }
  }
}